package de.lusiardi.webdav.xml;

import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

/**
 * http://www.webdav.org/specs/rfc4918.html#ELEMENT_status
 *
 * @author Joachim Lusiardi
 */
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Status {

    @XmlValue
    private String status;

    private static final Logger logger = Logger.getLogger(Status.class.getCanonicalName());

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
        if (!checkFormat()) {
            logger.warning(String.format("'%s' is an invalid status line.", status));
        }
    }

    public boolean checkFormat() {
        if (status == null) {
            return false;
        }
        //HTTP/1.1 403 Forbidden
        Pattern datePattern = Pattern.compile("^HTTP/\\d\\.\\d \\d\\d\\d .*$");
        Matcher dateMatcher = datePattern.matcher(status);

        if (!dateMatcher.matches()) {
            return false;
        }
        return true;
    }
}
